package com.ruoyi.cms.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.cms.domain.CmsSiteBanner4;
import com.ruoyi.cms.domain.CmsSiteHero;
import com.ruoyi.cms.domain.vo.CmsSiteAppVo;
import com.ruoyi.cms.domain.vo.CmsSiteModule4Vo;
import com.ruoyi.cms.service.ICmsSiteBanner4Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsSiteModule4Mapper;
import com.ruoyi.cms.domain.CmsSiteModule4;
import com.ruoyi.cms.service.ICmsSiteModule4Service;

/**
 * 小程序开发-模块信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-01
 */
@Service
public class CmsSiteModule4ServiceImpl implements ICmsSiteModule4Service
{
    @Autowired
    private CmsSiteModule4Mapper cmsSiteModule4Mapper;

    @Autowired
    private ICmsSiteBanner4Service cmsSiteBanner4Service;

    /**
     * 查询小程序开发-模块信息
     *
     * @param id 小程序开发-模块信息主键
     * @return 小程序开发-模块信息
     */
    @Override
    public CmsSiteModule4 selectCmsSiteModule4ById(Long id)
    {
        return cmsSiteModule4Mapper.selectCmsSiteModule4ById(id);
    }

    /**
     * 查询小程序开发-模块信息列表
     *
     * @param cmsSiteModule4 小程序开发-模块信息
     * @return 小程序开发-模块信息
     */
    @Override
    public List<CmsSiteModule4> selectCmsSiteModule4List(CmsSiteModule4 cmsSiteModule4)
    {
        return cmsSiteModule4Mapper.selectCmsSiteModule4List(cmsSiteModule4);
    }

    /**
     * 新增小程序开发-模块信息
     *
     * @param cmsSiteModule4 小程序开发-模块信息
     * @return 结果
     */
    @Override
    public int insertCmsSiteModule4(CmsSiteModule4 cmsSiteModule4)
    {
        return cmsSiteModule4Mapper.insertCmsSiteModule4(cmsSiteModule4);
    }

    /**
     * 修改小程序开发-模块信息
     *
     * @param cmsSiteModule4 小程序开发-模块信息
     * @return 结果
     */
    @Override
    public int updateCmsSiteModule4(CmsSiteModule4 cmsSiteModule4)
    {
        return cmsSiteModule4Mapper.updateCmsSiteModule4(cmsSiteModule4);
    }

    /**
     * 批量删除小程序开发-模块信息
     *
     * @param ids 需要删除的小程序开发-模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteModule4ByIds(Long[] ids)
    {
        return cmsSiteModule4Mapper.deleteCmsSiteModule4ByIds(ids);
    }

    /**
     * 删除小程序开发-模块信息信息
     *
     * @param id 小程序开发-模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteModule4ById(Long id)
    {
        return cmsSiteModule4Mapper.deleteCmsSiteModule4ById(id);
    }

    @Override
    public List<CmsSiteModule4Vo> getAllInfo() {

        List<CmsSiteModule4> cmsSiteModule4s = this.selectCmsSiteModule4List(null);
        List<CmsSiteBanner4> cmsSiteBanner4s = cmsSiteBanner4Service.selectCmsSiteBanner4List(null);

        //封装数据
        List<CmsSiteModule4Vo> collect = cmsSiteModule4s.stream().map(module -> {
            CmsSiteModule4Vo cmsSiteWebsitVo = new CmsSiteModule4Vo();
            BeanUtils.copyProperties(module, cmsSiteWebsitVo);
            List<CmsSiteBanner4> cmsSiteBanners1 = new ArrayList<>();
            cmsSiteBanner4s.stream().forEach(banner -> {
                if (module.getId().equals(banner.getApplistId())) {
                    cmsSiteBanners1.add(banner);
                }
            });
            //放之前排序
            cmsSiteBanners1.sort(Comparator.comparing(CmsSiteBanner4::getSort));
            cmsSiteWebsitVo.setCmsSiteBanner4s(cmsSiteBanners1);
            return cmsSiteWebsitVo;
        }).collect(Collectors.toList());

        //返回前排序
        collect.sort(Comparator.comparing(CmsSiteModule4Vo::getSort));

        return collect;
    }
}
